package com.century.sjt.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.util.Base64Utils;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:周玲波
 * @Description: 修改登陆密码
 * @date : 2016/8/18 22:02
 */
public class Setting_pwdActivity extends BaseActivity {
    private EditText Old_Pwd, New_Pwd, New_Pwd2;
    private Button Btn_Submit;
    private String NewPassword, OldPassWord;
    private RequestQueue mQueue;
    private TextView Forget_Pwd;
    private SjtTitleBar titleBar;
    private TextView original_pwd_icon, new_pwd_icon, again_new_pwd_icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjt_modify_pws_activity);
        mQueue = Volley.newRequestQueue(this);
        initView();
    }

    @Override
    protected void initView() {
        Old_Pwd = (EditText) findViewById(R.id.t_oldpwd);
        New_Pwd = (EditText) findViewById(R.id.t_newpwd);
        New_Pwd2 = (EditText) findViewById(R.id.t_newpwd2);
        Forget_Pwd = (TextView) findViewById(R.id.text_wangjiMM);
        Btn_Submit = (Button) findViewById(R.id.btn_sunmit_pwd);
        titleBar = (SjtTitleBar) findViewById(R.id.set_modify_pwd_title);
        titleBar.setLeftIconResource(getResources().getString(R.string.icon_return));
        titleBar.setTitle(getResources().getString(R.string.tab_pwdxg));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
       Typeface iconfont= Typeface.createFromAsset(getAssets(),"iconfont.ttf");
        original_pwd_icon = (TextView) findViewById(R.id.original_pwd_icon);
        new_pwd_icon = (TextView) findViewById(R.id.new_pwd_icon);
        again_new_pwd_icon = (TextView) findViewById(R.id.again_new_pwd_icon);
        original_pwd_icon.setTypeface(iconfont);
        new_pwd_icon.setTypeface(iconfont);
        again_new_pwd_icon.setTypeface(iconfont);

        Btn_Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NewPassword = New_Pwd.getText().toString();
                OldPassWord = Old_Pwd.getText().toString();
                if (OldPassWord == null || OldPassWord.equals("")) {
                    TipUtil.showToast(getResources().getString(R.string.error_no_old_password), Setting_pwdActivity.this, 1);
                } else if (NewPassword == null || NewPassword.equals("")) {
                    TipUtil.showToast(getResources().getString(R.string.error_no_new_password), Setting_pwdActivity.this, 1);
                } else if (NewPassword.length() < 6 && NewPassword.length() > 16) {
                    TipUtil.showToast(getResources().getString(R.string.error_is_6or16_new_password), Setting_pwdActivity.this, 1);
                } else if (!(New_Pwd2.getText().toString().equals(NewPassword))) {
                    TipUtil.showToast(getResources().getString(R.string.error_again_password), Setting_pwdActivity.this, 1);
                } else {
                    queryPassWard();
                }

            }
        });
        Forget_Pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("setpwd", "0");
                intent.setClass(Setting_pwdActivity.this, Forget_pwdActivity.class);
                startActivity(intent);
            }
        });


    }

    private void queryPassWard() {

        byte[] encodePwd = NewPassword.getBytes();
        final String npwd = Base64Utils.encode(encodePwd);
        byte[] oldPwd = OldPassWord.getBytes();
        final String olpwd = Base64Utils.encode(oldPwd);
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), Setting_pwdActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.SetPassWord, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    TipUtil.closeProgressDialog();
                    TipUtil.log("获取成功", response.toString(), 1);
                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    Log.d("code", code);
                    String message = res.getString("message");
                    if (code.equals("200")) {//成功
//                        JSONObject data = res.getJSONObject("data");
//                        String password = data.getString("password");
//                        Log.d("pwd",password);
                        TipUtil.showToast(getResources().getString(R.string.error_set_success), Setting_pwdActivity.this, 1);

                        SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                        SharedPreferences.Editor editor = mainSharedPreferences.edit();
                        editor.putString("isLogin", "0");
                        editor.commit();

                        Intent intent = new Intent(Setting_pwdActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);


                    } else {//失败
                        TipUtil.showToast(message, Setting_pwdActivity.this, 1);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_service), Setting_pwdActivity.this, 1);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), Setting_pwdActivity.this, 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                map.put("loginId", userId);
                map.put("password", npwd.trim());
                map.put("oldPassword", olpwd.trim());
                Log.d("top", map.toString());
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);
    }

    @Override
    protected void onDestroy() {
        mQueue.cancelAll(this);
        super.onDestroy();
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }
}
